#!/usr/bin/env python
# -*- coding:utf-8 -*-

import time
import json
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from lxml import etree


desired_capabilities = DesiredCapabilities.FIREFOX
desired_capabilities['loggingPrefs'] = {'performance': 'ALL'}


class AutoSpider(object):
    def __init__(self):
        self.init_browder()
        self.company_info_dict = {}
        self.start_year = 2022

    def init_browder(self):
        options = webdriver.FirefoxOptions()
        options.add_argument("disable-blink-features=AutomationControlled")  # 就是这一行告诉chrome去掉了webdriver痕迹
        self.browser = webdriver.Firefox(executable_path='./geckodriver.exe',
                                    options=options, desired_capabilities=desired_capabilities)
        self.wai = WebDriverWait(self.browser, 20)  # 等待20s后抛出异常

    def login(self,username="xiaozhou@gzxx",pwd='Aa123456'):
        username_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH, '//input[@class="el-input__inner" and @placeholder="请填写用户名/手机号"]')))
        pwd_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH, '//input[@class="el-input__inner" and @placeholder="请填写密码"]')))
        submit_elem = self.wai.until(EC.element_to_be_clickable(
            (By.XPATH, '//button[contains(@class,"el-button--primary")]')))
        # 使用账号密码登陆
        username_elem.send_keys(username)
        pwd_elem.send_keys(pwd)
        submit_elem.click()
        time.sleep(5)

    def close_alert(self):
        first_alter_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,
                                                                 '//div[@class="antd-pro-app-src-pages-application-search-vistor-components-xkb-active-modal-index-arrow-delete"]')))
        first_alter_elem.click()
        alter_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,
                                                           '//div[@class="antd-pro-app-src-pages-application-search-vistor-components-favorite-guide-index-close"]')))
        alter_elem.click()
        button_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH, '//button[@class="driver-close-btn"]')))
        button_elem.click()
        time.sleep(1)

    def change_search_param(self,left_times=1,right_times=1):
        ls = self.browser.find_elements_by_xpath('//div[contains(@class,"el-date-editor--date")]/input[@class="el-input__inner"]')
        print(ls)
        start_time_elem, end_time_elem = ls
        start_time_elem.click()

        left_last_year_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//div[@x-placement="bottom-start"]//button[contains(@class,"el-date-picker__prev-btn") and @aria-label="前一年"]')))
        # right_last_year_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//button[contains(@class,"el-date-picker__prev-btn") and @aria-label="前一年"]')))
        for i in range(left_times):
            left_last_year_elem.click()
        # for i in range(right_times):
        #     right_last_year_elem.click()
        left_day_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//div[@x-placement="bottom-start"]//tr[@class="el-date-table__row"]/td[contains(@class,"available")]/div/span[text()=1]')))
        left_day_elem.click()
        end_time_elem.click()
        right_last_year_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,
                                                                         '//div[@x-placement="bottom-start"]//button[contains(@class,"el-date-picker__prev-btn") and @aria-label="前一年"]')))
        for i in range(right_times):
            right_last_year_elem.click()
        right_day_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,
                                                                   '//div[@x-placement="bottom-start"]//tr[@class="el-date-table__row"]/td[contains(@class,"available")]/div/span[text()=1]')))
        right_day_elem.click()
        search_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//button[contains(@class,"el-button--primary")]/span[contains(text(),"查找")]')))
        search_elem.click()
        time.sleep(1)

    def wait_company_info_lode(self):
        self.wai.until(EC.element_to_be_clickable((By.XPATH,'//table[@class="el-table__body"]')))

        time.sleep(1)

    def save_company_info(self):
        # 解锁
        checkbox_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//thead[@class="has-gutter"]//label[@class="el-checkbox"]//span[@class="el-checkbox__inner"]')))
        checkbox_elem.click()
        release_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,
            '//button[contains(@class,"el-button--primary")]/span[contains(text(),"解锁领取")]')))
        release_elem.click()
        stuff_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//label[@class="el-form-item__label" and contains(text(),"指定员工")]/following-sibling::div//input[@class="el-input__inner"]')))
        stuff_elem.click()
        company_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//span[@class="el-cascader-node__label" and contains(text(),"广州")]')))
        company_elem.click()
        person_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//span[@class="el-cascader-node__label" and contains(text(),"xiao")]')))
        person_elem.click()
        time.sleep(2)
        release_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,
            '//button[contains(@class,"el-button--primary")]/span[contains(text(),"解锁并领取")]')))
        release_elem.click()

        close_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,
            '//button[contains(@class,"el-button--default")]/span[contains(text(),"关闭")]')))
        time.sleep(10)
        close_elem.click()
        time.sleep(5)
        # self.save_list_info(self.browser.page_source)
        tr_elems = self.browser.find_elements_by_xpath('//tr[@class="el-table__row"]')
        print(tr_elems)
        is_end = False
        for index,elem in enumerate(tr_elems):
            time.sleep(1)
            try:
                if not is_end:
                    self.browser.execute_script("arguments[0].scrollIntoView();", elem)
                    ActionChains(self.browser).move_to_element(elem).click().perform()
            except:
                print('翻到底了，跳过')
                is_end = True
            time.sleep(3)
            try:
                lock_elem = self.browser.find_element_by_xpath('//div[@class="filter-model"]/p[contains(text(),"点击解锁")]')
                if lock_elem:
                    lock_elem.click()
                    btn_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//button[contains(@class,"el-button--primary")]/span[contains(text(),"解锁并领取")]')))
                    btn_elem.click()
                    time.sleep(5)

            except:
                pass
            file_name = '机制云/' + str(time.time())
            with open(file_name, 'w', encoding='utf-8') as f:
                f.write(self.browser.page_source)
                self.parse_source_code(self.browser.page_source)
            try:
                self.browser.back()
            except:
                print('无回头')
            time.sleep(5)
            tr_elems = self.browser.find_elements_by_xpath('//tr[@class="el-table__row"]')

    def save_list_info(self,text):
        '//tr[@class="el-table__row"]//p[contains(@class,"ent-name")]'
        selector = etree.HTML(text, parser=etree.HTMLParser(encoding='utf-8'))
        tr_selector_ls = selector.xpath('//tr[@class="el-table__row"]')
        for tr_selector in tr_selector_ls:
            ent_name = ''.join(tr_selector.xpath('.//p[contains(@class,"ent-name")]/span/text()')).strip()
            print(ent_name)
            start_year = ''.join(tr_selector.xpath('.//td[contains(@class,"el-table_1_column_4 is-center")]//text()')).strip()
            self.company_info_dict[ent_name] = {'start_year':start_year}
            self.start_year = start_year
        print(self.company_info_dict)




    def parse_source_code(self,text):
        print('解析源码')
        selector = etree.HTML(text, parser=etree.HTMLParser(encoding='utf-8'))
        company_name = ''.join(selector.xpath('//p[@class="ent-name"]/text()')).strip()
        try:
            start_year = ''.join(selector.xpath('//tr[@class="el-descriptions-row"]/th[contains(text(),"成立日期")]/../td[position()=1]')).strip()
            if not self.company_info_dict.get('company_name'):
                self.company_info_dict[company_name] = {'start_year':start_year}
        except:
            if not self.company_info_dict.get('company_name'):
                self.company_info_dict[company_name] = {'start_year':self.start_year}

        div_selectors = selector.xpath('//div[@class="AiTokerClueContactBlock"]')
        print(div_selectors)
        phone_count = 0
        save_ls = []
        for div_selector in div_selectors:

            is_phone = div_selector.xpath('./div//div[@class="SeatCallRobotPhoneItem"]')
            print('is_phone',is_phone)
            if not is_phone:
                continue

            phone_ls = div_selector.xpath('./div//div[@class="ContactInfo-top-contact"]/p/text()')
            phone = ''.join(phone_ls).strip()
            print(phone)
            if '-' in phone or len(phone)!=11:
                continue
            source_name = div_selector.xpath('./div//div[@class="Source-link"]/span/text()')
            source_name_ls = div_selector.xpath('./div//div[@class="Source-link"]/a/text()')
            # source_list = div_selector.xpath('./div//div[@class="Source-list"]//text()')
            source_list = div_selector.xpath('./div//div[@class="Source-list"]//td[contains(@class,"el-table__cell")]//div[@class="cell"]//text()')
            source_list_2 = div_selector.xpath('./div//div[@class="Source-list"]//span[@class="el-popover__reference-wrapper"]//text()')
            source_name_ls.append(source_name)
            source_name_ls.extend(source_list)
            source_name_ls.extend(source_list_2)
            source_href_ls = div_selector.xpath('./div//div[@class="Source-link"]/a/@href')
            print(company_name, phone_ls, source_name_ls,source_href_ls)
            print('==============================')
            save_ls.append([source_name_ls,source_href_ls,''.join(phone_ls)])
            phone_count += 1
            if not self.company_info_dict[company_name].get('phone_ls'):
                self.company_info_dict[company_name]['phone_ls'] = []
            self.company_info_dict[company_name]['phone_ls'].append({
                'phone':phone,
                'source_ls':source_name_ls,
                'source_href_ls':source_href_ls
            })
            print(self.company_info_dict[company_name])
            print('记录成功')
        if  self.company_info_dict.get(company_name):
            self.company_info_dict[company_name]['phone_count'] = phone_count
        else:
            self.company_info_dict[company_name] = {
                'start_year':self.start_year,
                'phone_count':phone_count

            }

        for ls in save_ls:
            with open('jz_info.csv', 'a', encoding='utf-8') as f:
                start_year = self.company_info_dict[company_name].get('start_year')
                f.write(f'{company_name},{start_year},{phone_count},{ls[2]},{str(ls[0])},{str(ls[1])}\n')





    def main(self):
        self.browser.maximize_window()
        self.browser.get('https://business.steponeai.com/login')
        # 登录
        self.login()
        # 进入高级搜索
        for i in range(7):
            self.browser.get('https://business.steponeai.com/toker/save_group/index')
            use_button_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//button[contains(@class,"el-button--text")]/span[contains(text(),"使用")]')))
            use_button_elem.click()
            time.sleep(5)
            # 更改搜索条件
            self.start_year -= i
            self.change_search_param(left_times=i,right_times=i)
            # # time.sleep(1000)
            # 等待 公司信息加载
            try:
                self.wait_company_info_lode()
            except:
                print('找到0条企业')
                continue
            self.save_company_info()
            time.sleep(5)
        with open('result_jzy.json','w',encoding='utf-8') as f:
            json.dump(self.company_info_dict,f,ensure_ascii=False,indent=4)



if __name__ == '__main__':
    spider = AutoSpider()
    spider.main()
    result = {
        '广州探迹科技有限公司':{
            'start_year':'2021-05-31',
            'phone_count':4,
            'phone_ls':[
                {
                    'phone':'17709002923',
                    'source_ls':[],
                    'source_href_ls':[]
                }
            ]
        }
    }
